package editStudentGui;

import gui.GUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableRowSorter;

import seminar.Seminar;

@SuppressWarnings("serial")
public class EditStudentLayeredPane extends JLayeredPane implements
		ActionListener, TableModelListener
{

	private static JTable studentTable;
	private JButton newButton;
	private JButton deleteButton;
	private JButton tradeButton;
	private EditStudentTableModel mymodel = new EditStudentTableModel();

	public EditStudentLayeredPane()
	{

		this.setLayout(new BorderLayout(0, 0));

		JPanel mainPanel = new JPanel();
		this.add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new BorderLayout(0, 0));

		JPanel leftPanel = new JPanel();
		mainPanel.add(leftPanel, BorderLayout.CENTER);
		leftPanel.setLayout(new BorderLayout(0, 0));

		JPanel leftNorthPanel = new JPanel();
		leftPanel.add(leftNorthPanel, BorderLayout.NORTH);
		leftNorthPanel.setLayout(new GridLayout(0, 6, 0, 0));

		JPanel leftSouthPanel = new JPanel();
		leftPanel.add(leftSouthPanel, BorderLayout.SOUTH);
		leftSouthPanel.setLayout(new BorderLayout(0, 0));

		JPanel leftSouthSouthPanel = new JPanel();
		leftSouthPanel.add(leftSouthSouthPanel);
		leftSouthSouthPanel.setLayout(new BorderLayout(0, 0));

		deleteButton = new JButton("Löschen");
		leftSouthSouthPanel.add(deleteButton, BorderLayout.WEST);
		deleteButton.setEnabled(false);

		newButton = new JButton("Neu");
		leftSouthSouthPanel.add(newButton, BorderLayout.EAST);
		newButton.setEnabled(true);

		tradeButton = new JButton("Tauschen");
		leftSouthSouthPanel.add(tradeButton, BorderLayout.CENTER);
		tradeButton.setEnabled(false);

		tradeButton.addActionListener(this);
		newButton.addActionListener(this);
		deleteButton.addActionListener(this);

		JPanel leftWestPanel = new JPanel();
		leftPanel.add(leftWestPanel, BorderLayout.WEST);
		leftWestPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel leftCenterPanel = new JPanel();
		leftPanel.add(leftCenterPanel, BorderLayout.CENTER);
		leftCenterPanel.setLayout(new BorderLayout(0, 0));

		JPanel leftCenterCenterPanel = new JPanel();
		leftCenterPanel.add(leftCenterCenterPanel, BorderLayout.CENTER);
		leftCenterCenterPanel.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		leftCenterCenterPanel.add(scrollPane, BorderLayout.CENTER);

		studentTable = new JTable(mymodel);
		studentTable
				.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		studentTable.getColumnModel().getColumn(0).setPreferredWidth(5);
		studentTable.getColumnModel().getColumn(1).setPreferredWidth(5);
		scrollPane.setViewportView(studentTable);
		mymodel.addTableModelListener(this);
		studentTable.addMouseListener(FreeAllTheButtons());

		TableRowSorter<EditStudentTableModel> sorter = new TableRowSorter<EditStudentTableModel>(
				mymodel);

		studentTable.setRowSorter(sorter);
	}

	private MouseListener FreeAllTheButtons()
	{
		return new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent evt)
			{
				updateButtons();
			}
		};
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == deleteButton)
		{
			mymodel.delete(studentTable.convertRowIndexToModel(studentTable
					.getSelectedRow()));
			GUI.statusausgabe("Student gelöscht");
		}
		if (e.getSource() == newButton)
		{
			Seminar.addDefaultStudent();
			GUI.statusausgabe("Neuer Student angelegt");
			mymodel.update();
		}
		if (e.getSource() == tradeButton)
		{
			mymodel.trade();
			GUI.statusausgabe("Es wurde getauscht");
			mymodel.update();
		}
	}

	/**
	 * Wird aufgerufen, wenn sich in der Tabelle was geändert hat.
	 */
	@Override
	public void tableChanged(TableModelEvent e)
	{
		updateButtons();
	}

	/**
	 * Überprüft, welche Buttons aktiv sein dürfen
	 */
	public void updateButtons()
	{
		// Tauschen-Button
		if (((mymodel.numberOfChoose(0) != -1) && (mymodel.numberOfChoose(1)) != -1)
				&& (mymodel.numberOfChoose(0) != mymodel.numberOfChoose(1)))
			tradeButton.setEnabled(true);
		else
			tradeButton.setEnabled(false);
		// Löschen-Button
		if (studentTable.getSelectedRow() > -1
				&& studentTable.getSelectedColumn() > 1)
			deleteButton.setEnabled(true);
		else
			deleteButton.setEnabled(false);
	}

	public void updateStudentTable()
	{
		mymodel.update();

	}
}